package com.sunch.mapper;

import com.sunch.pojo.Home;
import com.sunch.pojo.Room;
import com.sunch.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;


@Mapper
public interface UserMapper {
    /**
     * 根据用户名和密码校验登录
     * @param user
     * @return
     */
    @Select("select * from user where username = #{username} and password = #{password} ")
    User getByLoginNameAndPassword(User user);

    /**
     * 注册时查询当前用户是否存在
     * @param user
     * @return
     */
    @Select("select * from user where username = #{username}")
    User getByLoginName(User user);

    /**
     * 注册用户
     * @param user
     */
    @Insert("insert into user (username, password, phone) values (#{username}, #{password}, #{phone})")
    void insertByNameAndPassword(User user);

    /**
     * 添加我的家
     * @param home
     */
    @Insert("insert into my_home (user_id, home_name, default_home) values (#{userId}, #{homeName}, #{defaultHome})")
    void addHome(Home home);

    /**
     * 查询我的所有家
     * @param userId
     * @return
     */
    @Select("select * from my_home where user_id = #{userId}")
    List<Home> getMyHome(int userId);

    /**
     * 添加我的房间
     * @param room
     */
    @Insert("insert into rooms (user_id, room_name, home_id) values (#{userId}, #{roomName}, #{homeId});")
    void addRoom(Room room);

    /**
     * 根据家id和用户id查询所有房间
     * @param userId
     * @param homeId
     * @return
     */
    @Select("select * from rooms where rooms.user_id = #{userId} and home_id = #{homeId}")
    List<Room> getMyRoom(int userId, int homeId);
}
